package com.longcai.piaojia.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.GetCodePost;
import com.longcai.piaojia.conn.RegisterPost;
import com.longcai.piaojia.dialog.RegistSuccessDialog;
import com.longcai.piaojia.util.MEditTextListener;
import com.longcai.piaojia.util.MyToast;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilMatches;

/**
 * Created by Administrator on 2016/8/2.
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener {


    @BoundView(R.id.register_user_account_et) private EditText register_user_account_et;
    @BoundView(R.id.register_verification_code_et) private EditText register_verification_code_et;
    @BoundView(R.id.register_user_password_et) private EditText register_user_password_et;
    @BoundView(R.id.register_cofirm_user_password_et) private EditText register_cofirm_user_password_et;
    @BoundView(R.id.register_recommend_et) private EditText register_recommend_et;

    @BoundView(R.id.register_verification_code_tv) private TextView register_verification_code_tv;
    @BoundView(R.id.register_sumbit_tv) private TextView register_sumbit_tv;

    private GetCodePost.CodeEntity codeEntityRegister;
    private int netCode;

    private CountDownTimer countTime = new CountDownTimer(61000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

            if(!isFinishing()) {

                register_verification_code_tv.setText(millisUntilFinished / 1000
                        + getString(R.string.edit_phonenum_code_count_down_time_text));

            }
        }

        @Override
        public void onFinish() {

            if(!isFinishing()) {
                register_verification_code_tv.setText(getString(R.string.edit_phonenum_code_text));
                register_verification_code_tv.setEnabled(true);
            }
        }
    };

    private GetCodePost getCodePost = new GetCodePost(new AsyCallBack<GetCodePost.CodeEntity>(){

        @Override
        public void onSuccess(String toast, int type, GetCodePost.CodeEntity codeEntity) throws Exception {
            super.onSuccess(toast, type, codeEntity);

            codeEntityRegister = codeEntity;

            netCode = codeEntity.code;

            Toast.makeText(RegisterActivity.this, toast, Toast.LENGTH_LONG).show();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            Toast.makeText(RegisterActivity.this, toast, Toast.LENGTH_LONG).show();

            countTime.cancel();
            register_verification_code_tv.setEnabled(true);
            register_verification_code_tv.setText(getString(R.string.register_SMS_verification_code_text));

        }
    });

    private RegisterPost registerPost = new RegisterPost(new AsyCallBack<String>(){

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {
            super.onSuccess(toast, type, s);

//            BaseApplication.BasePreferences.saveUserId(s);
//
//            BaseApplication.BasePreferences.saveUserAccount(register_user_account_et.getText().toString().trim());
//
//            BaseApplication.BasePreferences.saveIsLogin(true);
//
//            BaseApplication.INSTANCE.finishActivity(LoginActivity.class, RegisterActivity.class);

//            Toast.makeText(RegisterActivity.this, toast, Toast.LENGTH_LONG).show();

            RegistSuccessDialog registSuccessDialog = new RegistSuccessDialog(context, s) {
                @Override
                protected void onConfirm() {

                    dismiss();

                    finish();

                }
            };

            registSuccessDialog.setCanceledOnTouchOutside(false);

            registSuccessDialog.show();


        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            Toast.makeText(RegisterActivity.this, toast, Toast.LENGTH_LONG).show();


        }

    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_register);

        setBackTrue();

        setTitleName(getResources().getString(R.string.register_title_text));

        register_verification_code_tv.setOnClickListener(this);
        register_sumbit_tv.setOnClickListener(this);

        register_user_password_et.addTextChangedListener(new MEditTextListener(context, 16, register_user_password_et));
        register_cofirm_user_password_et.addTextChangedListener(new MEditTextListener(context, 16, register_cofirm_user_password_et));

    }

    @Override
    public void onClick(View view) {

        String phone = register_user_account_et.getText().toString().trim();
        String code = register_verification_code_et.getText().toString().trim();
        String password = register_user_password_et.getText().toString().trim();
        String confirmPassword = register_cofirm_user_password_et.getText().toString().trim();
        String recommendPerson = register_recommend_et.getText().toString().trim();

        switch (view.getId()){

            case R.id.register_verification_code_tv:

                if (TextUtils.isEmpty(phone)) {

                    MyToast.show(context, getString(R.string.warning_please_fill_phone_text), Toast.LENGTH_LONG);

                    return;

                } else if (!UtilMatches.checkMobile(phone)) {

                    MyToast.show(context, getString(R.string.warning_please_fill_true_phone_text), Toast.LENGTH_LONG);

                    return;

                }

                getCodePost.phone = phone;
                getCodePost.type = "reg";
                getCodePost.change = "";
                getCodePost.execute(context);

                register_verification_code_tv.setEnabled(false);

                countTime.start();

                break;

            case R.id.register_sumbit_tv:

                if (TextUtils.isEmpty(phone)) {

                    MyToast.show(context, getString(R.string.login_user_account_hint), Toast.LENGTH_LONG);

                    return;

                } else if (!UtilMatches.checkMobile(phone)) {

                    MyToast.show(context, getString(R.string.warning_please_fill_true_phone_text), Toast.LENGTH_LONG);

                    return;

                } else if (netCode == 0) {

                    MyToast.show(context, getString(R.string.warning_please_click_get_code_text), Toast.LENGTH_LONG);

                    return;

                } else if (TextUtils.isEmpty(code)) {

                    MyToast.show(context, getString(R.string.register_verification_code_hint), Toast.LENGTH_LONG);

                    return;

                } else if (Integer.parseInt(code) != codeEntityRegister.code) {

                    MyToast.show(context, getString(R.string.warning_bad_code_text), Toast.LENGTH_LONG);

                    return;

                } else if (TextUtils.isEmpty(password)) {

                    MyToast.show(context, getString(R.string.warning_set_password_text), Toast.LENGTH_LONG);

                    return;

                } else if (password.length() < 6 || password.length() > 20) {

                    MyToast.show(context, getString(R.string.warning_please_limit_password_length_text), Toast.LENGTH_LONG);

                    return;

                } else if (TextUtils.isEmpty(confirmPassword)) {

                    MyToast.show(context, getString(R.string.warning_please_fill_confirmpassword_text), Toast.LENGTH_LONG);

                    return;

                } else if (!password.equals(confirmPassword)) {

                    MyToast.show(context, getString(R.string.warning_password_not_same_text), Toast.LENGTH_LONG);

                    return;

                } else if (!TextUtils.isEmpty(recommendPerson)&&!UtilMatches.checkMobile(recommendPerson)) {

                    MyToast.show(context, getString(R.string.warning_please_fill_true_recommend_phone_text), Toast.LENGTH_LONG);

                    return;

                }

                registerPost.phone = phone;
                registerPost.user_pass = password;
                registerPost.code = code;
                registerPost.inviter_id = TextUtils.isEmpty(recommendPerson)?"0":recommendPerson;
                registerPost.execute(RegisterActivity.this);

                break;

        }
    }

}
